﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblPrivilegeDAL:ImtblPrivilege
    {
        HKEA_CMSEntities model;
        mtblPrivilege privilege;
        public mtblPrivilegeDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblPrivilegeDAL GetInstance()
        {
            return new mtblPrivilegeDAL();
        }
        public int InsertENT(mtblPrivilege ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblPrivileges.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblPrivilege> GetListByENT(mtblPrivilege ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblPrivileges.Where(a => a.PrivilegeUID > 0);
            if (ent.PrivilegeUID > 0)
                result = result.Where(a => a.PrivilegeUID == ent.PrivilegeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblPrivilege> GetListByENT(mtblPrivilege ent)
        {
            var result = model.mtblPrivileges.Where(a => a.PrivilegeUID > 0);
            if (ent.PrivilegeUID > 0)
                result = result.Where(a => a.PrivilegeUID == ent.PrivilegeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblPrivilege ent)
        {
            var result = model.mtblPrivileges.Where(a => a.PrivilegeUID > 0);
            if (ent.PrivilegeUID > 0)
                result = result.Where(a => a.PrivilegeUID == ent.PrivilegeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblPrivilege ent)
        {
            privilege = model.mtblPrivileges.Where(a=>a.PrivilegeUID==ent.PrivilegeUID).FirstOrDefault();
            if (privilege != null)
            {
                privilege.Deleted = ent.Deleted;
                privilege.LastModifyBy = ent.LastModifyBy;
                privilege.LastModifyDate = DateTime.Now;
                privilege.Privilege_Chi = ent.Privilege_Chi;
                privilege.Privilege_Eng = ent.Privilege_Eng;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblPrivilege ent)
        {
            privilege = model.mtblPrivileges.Where(a => a.PrivilegeUID == ent.PrivilegeUID).FirstOrDefault();
            if (privilege != null)
            {
                model.mtblPrivileges.DeleteObject(privilege);
            }
            return model.SaveChanges();
        }
    }
}
